# -*- coding: utf-8 -*-
'''
Descrição: Interface para o Rebides em Jython
Data: 09/12/11
Autor: Luís Costa Nº 6032
'''

from javax.swing import *
from java.awt import *

from Main import Main

class Rebides(JFrame):

   
    #Construtor da classe
    #-------------------------
    def __init__(self):
        
        #Ligaçao à classe principal
        #---------------
        self.main = Main()
        
        self.frame = JFrame("Rebides - Jython")
        self.frame.setSize(400, 300)
        self.createMenus()
        self.addItemsToFrame()
                
        #set window visible, aligned on screen and to front
        self.pack()
        self.visible = True
        self.toFront()
    pass
    #------------------------
    
    #Cria o menu bar e adiciona as entradas do menu
    #------------------------
    def createMenus(self):
        
        menubar = JMenuBar()
        
        #----------------------
        #Menu Ficheiro
        #----------------------
        menuFicheiro = JMenu('Ficheiro')
        
        # Item - Sair
        menuItemSair = JMenuItem('Sair', actionPerformed=self.terminateProgram)
        menuFicheiro.add(menuItemSair)
        #---------------------
        menubar.add(menuFicheiro)
        #----------------------
        
        #----------------------
        #Menu Recolher Dados
        #----------------------
        menuGetData = JMenu('Recolher Dados')
        
        # Item - Year 0
        menuItemYear0 = JMenuItem('Ano 2000', actionPerformed=self.evtcmdYear2000)
        menuGetData.add(menuItemYear0)
        
        # Item - Year 1
        menuItemYear1 = JMenuItem('Ano 2001', actionPerformed=self.evtcmdYear2001)
        menuGetData.add(menuItemYear1)
        
        # Item - Year 2
        menuItemYear2 = JMenuItem('Ano 2002', actionPerformed=self.evtcmdYear2002)
        menuGetData.add(menuItemYear2)
        
        # Item - Year 3
        menuItemYear3 = JMenuItem('Ano 2003', actionPerformed=self.evtcmdYear2003)
        menuGetData.add(menuItemYear3)
        
        # Item - Year 4
        menuItemYear4 = JMenuItem('Ano 2004', actionPerformed=self.evtcmdYear2004)
        menuGetData.add(menuItemYear4)
        
        # Item - Year 5
        menuItemYear5 = JMenuItem('Ano 2005', actionPerformed=self.evtcmdYear2005)
        menuGetData.add(menuItemYear5)
        
        # Item - Year 6
        menuItemYear6 = JMenuItem('Ano 2006', actionPerformed=self.evtcmdYear2006)
        menuGetData.add(menuItemYear6)
        
        # Item - Year 7
        menuItemYear7 = JMenuItem('Ano 2007', actionPerformed=self.evtcmdYear2007)
        menuGetData.add(menuItemYear7)
        
        # Item - Year 8
        menuItemYear8 = JMenuItem('Ano 2008', actionPerformed=self.evtcmdYear2008)
        menuGetData.add(menuItemYear8)
        
        # Item - Year 9
        menuItemYear9 = JMenuItem('Ano 2009', actionPerformed=self.evtcmdYear2009)
        menuGetData.add(menuItemYear9)
        
        # Item - Year 10
        menuItemYear10 = JMenuItem('Ano 2010', actionPerformed=self.evtcmdYear2010)
        menuGetData.add(menuItemYear10)
        
        # Item - Year All
        menuItemYearAll = JMenuItem('Todos os Anos', actionPerformed=self.evtcmdYearAll)
        menuGetData.add(menuItemYearAll)
        #---------------------
        menubar.add(menuGetData)
        #----------------------
        
        #----------------------
        #Menu BD
        #----------------------
        menuDB = JMenu('Base Dados')
        
        # Item - CreateDB
        menuItemDB = JMenuItem('Criar BD', actionPerformed=self.evtcmdCreateDB)
        menuDB.add(menuItemDB)
        #---------------------
        menubar.add(menuDB)
        #----------------------
        
        #----------------------
        #Menu HTTP
        #----------------------
        menuHTTP = JMenu('HTTP')
        
        # Item - StartServer
        menuItemStartServer = JMenuItem('Ligar Servidor', actionPerformed=self.evtcmdStartServer)
        menuHTTP.add(menuItemStartServer)
        
        # Item - StopServer
        menuItemStopServer = JMenuItem('Desligar Servidor', actionPerformed=self.evtcmdStopServer)
        menuHTTP.add(menuItemStopServer)
        #---------------------
        menubar.add(menuHTTP)
        #----------------------

        self.setJMenuBar(menubar)
    pass
    #------------------------
    
    def addItemsToFrame(self):
       
        #Box vertical principal
        vBox = Box.createVerticalBox()

        vBox.add(self.estabStatFrame())
        vBox.add(self.catStatFrame())
        vBox.add(self.tDegreeStatFrame())
        
        vBox.add(self.createStatusIndicator('Pronto'))
                 
        self.add(vBox)
    pass
        
    #Cria uma box com as opções da estatistica dos professores por estabelecimento
    #------------------------------------
    def estabStatFrame(self):
        
        hBox = Box.createHorizontalBox()
        cmdStatEstab = JButton('Calcula Estatisticas', actionPerformed=self.evtcmdProfEstabStat)
        cmdCSVEstab = JButton('Cria CSV', actionPerformed=self.evtcmdProfEstabCSV)
        cmdListEstab = JButton('Cria Listagem', actionPerformed=self.evtcmdProfEstabList)
        
        hBox.add(cmdStatEstab)
        hBox.add(cmdCSVEstab)
        hBox.add(cmdListEstab)
        
        #Border para a box
        title = BorderFactory.createTitledBorder("Estatisticas Professores por Estabelecimento")
        hBox.setBorder(title)
        
        return hBox
    pass
    #------------------------------------
    
    #Cria uma box com as opções da estatistica dos professores por categoria
    #------------------------------------
    def tDegreeStatFrame(self):
        
        hBox = Box.createHorizontalBox()
        cmdStattDegree = JButton('Calcula Estatisticas', actionPerformed=self.evtcmdProfTDegreeStat)
        cmdCSVtDegre = JButton('Cria CSV', actionPerformed=self.evtcmdProfTDegreeCSV)
        cmdListtDegre = JButton('Cria Listagem', actionPerformed=self.evtcmdProfTDegreeList)
        
        hBox.add(cmdStattDegree)
        hBox.add(cmdCSVtDegre)
        hBox.add(cmdListtDegre)
        
        #Border para a box
        title = BorderFactory.createTitledBorder("Estatisticas Professores por tipo de grau")
        hBox.setBorder(title)
        
        return hBox
    pass
    #------------------------------------
    
    #Cria uma frame com as opções da estatistica dos professores por categoria
    #------------------------------------
    def catStatFrame(self):
        
        hBox = Box.createHorizontalBox()
        cmdStatCat = JButton('Calcula Estatisticas', actionPerformed=self.evtcmdProfCatStat)
        cmdCSVCat = JButton('Cria CSV', actionPerformed=self.evtcmdProfCatCSV)
        cmdListCat = JButton('Cria Listagem', actionPerformed=self.evtcmdProfCatList)
        
        hBox.add(cmdStatCat)
        hBox.add(cmdCSVCat)
        hBox.add(cmdListCat)
        
        #Border para a box
        title = BorderFactory.createTitledBorder("Estatisticas Professores por Categoria")
        hBox.setBorder(title)
        
        return hBox
    pass
    
    def createStatusIndicator(self, text):
        
        self.statusLabel = JLabel(text, JLabel.CENTER)
        self.statusLabel.setFont(Font("Serif", Font.BOLD, 18));
        
        self.statusPanel = JPanel()
        self.statusPanel.setBackground(Color.green)
        
        self.statusPanel.add(self.statusLabel)
        
        return self.statusPanel
    pass

    def setStatusProcessing(self, status, text):
        
        if status == True:
            self.statusPanel.setBackground(Color.red)
        else:
            self.statusPanel.setBackground(Color.green)
        pass
        
        self.statusLabel.setText(text)
        
        self.statusLabel.repaint()
        self.statusPanel.repaint()
        
        self.statusPanel.invalidate()
        self.statusLabel.invalidate()
        
        self.statusPanel.validate()
        self.statusLabel.validate()
        
        self.invalidate()
        self.validate()
    pass
    #----------------------------------
    #EVENTOS
    #----------------------------------
    def terminateProgram(self, event):
        exit(0)
    pass 

    def evtcmdYear2000(self, event): 

        self.setStatusProcessing (True, 'A processar os dados do ano 0...')
        self.main.get_rebides_data(0)
        self.setStatusProcessing (False, 'Processamento terminado!')

    def evtcmdYear2001(self, event): 

        self.setStatusProcessing (True, 'A processar os dados do ano 1...')
        self.main.get_rebides_data(1)

    def evtcmdYear2002(self, event): 

        self.setStatusProcessing (True,'A processar os dados do ano 2...')
        self.main.get_rebides_data(2)
        self.setStatusProcessing (False,'Processamento terminado!')

    def evtcmdYear2003(self, event): 

        self.setStatusProcessing (True,'A processar os dados do ano 3...')
        self.main.get_rebides_data(3)
        self.setStatusProcessing (False,'Processamento terminado!')

    def evtcmdYear2004(self, event): 

        self.setStatusProcessing (True,'A processar os dados do ano 4...')
        self.main.get_rebides_data(4)
        self.setStatusProcessing (False,'Processamento terminado!')

    def evtcmdYear2005(self, event): 

        self.setStatusProcessing (True,'A processar os dados do ano 5...')
        self.main.get_rebides_data(5)
        self.setStatusProcessing (False,'Processamento terminado!')

    def evtcmdYear2006(self, event): 

        self.setStatusProcessing (True,'A processar os dados do ano 6...')
        self.main.get_rebides_data(6)
        self.setStatusProcessing (False,'Processamento terminado!')

    def evtcmdYear2007(self, event): 

        self.setStatusProcessing (True,'A processar os dados do ano 7...')
        self.main.get_rebides_data(7)
        self.setStatusProcessing (False,'Processamento terminado!')

    def evtcmdYear2008(self, event): 

        self.setStatusProcessing (True,'A processar os dados do ano 8...')
        self.main.get_rebides_data(8)
        self.setStatusProcessing (False,'Processamento terminado!')

    def evtcmdYear2009(self, event): 

        self.setStatusProcessing (True,'A processar os dados do ano 9...')
        self.main.get_rebides_data(9)
        self.setStatusProcessing (False,'Processamento terminado!')

    def evtcmdYear2010(self, event): 

        self.setStatusProcessing (True,'A processar os dados do ano 10...')
        self.main.get_rebides_data(10)
        self.setStatusProcessing (False,'Processamento terminado!')

    def evtcmdYearAll(self, event): 

        self.setStatusProcessing (True,'A processar os dados de todos os anos...')
        self.main.get_rebides_data_all()
        self.setStatusProcessing (False,'Processamento terminado!')

    def evtcmdCreateDB(self, event): 

        self.setStatusProcessing (True,'A criar as tabelas da base de dados...')
        self.main.create_db_tables()
        self.setStatusProcessing (False,'Tabelas criádas com sucesso!')
            
    def evtcmdProfEstabStat(self, event): 

        self.setStatusProcessing (True,'A processar a estatistica...')
        self.main.get_stat_teachers_per_establishment()
        self.setStatusProcessing (False,'Estatistica processa com sucesso!')

    def evtcmdProfEstabCSV(self, event): 

        self.setStatusProcessing (True,'A criar o ficheiro CSV...')
        self.main.csv_teachers_per_establishment()
        self.setStatusProcessing (False,'Ficheiro CSV criádo com sucesso!')

    def evtcmdProfEstabList(self, event): 

        self.setStatusProcessing (True,'A criar a listagem em HTML...')
        self.main.list_teachers_per_establishment()
        self.setStatusProcessing (False,'Listagem em HTML criáda com sucesso!')

    def evtcmdProfCatStat(self, event): 

        self.setStatusProcessing (True,'A processar a estatistica...')
        self.main.get_stat_teachers_per_category_establishment_year()
        self.setStatusProcessing (False,'Estatistica processa com sucesso!')

    def evtcmdProfCatCSV(self, event): 

        self.setStatusProcessing (True,'A criar o ficheiro CSV...')
        self.main.csv_teachers_per_category()
        self.setStatusProcessing (False,'Ficheiro CSV criádo com sucesso!')

    def evtcmdProfCatList(self, event): 

        self.setStatusProcessing (True,'A criar a listagem em HTML...')
        self.main.list_teachers_per_category()
        self.setStatusProcessing (False,'Listagem em HTML criáda com sucesso!')

    def evtcmdProfTDegreeStat(self, event): 

        self.setStatusProcessing (True,'A processar a estatistica...')
        self.main.get_stat_teachers_per_type_degree_establishment_year()
        self.setStatusProcessing (False,'Estatistica processa com sucesso!')

    def evtcmdProfTDegreeCSV(self, event): 

        self.setStatusProcessing (True,'A criar o ficheiro CSV...')
        self.main.csv_teachers_per_tdegree()
        self.setStatusProcessing (False,'Ficheiro CSV criádo com sucesso!')
    
    def evtcmdProfTDegreeList(self, event): 

        self.setStatusProcessing (True,'A criar a listagem em HTML...')
        self.main.list_teachers_per_tdegree()
        self.setStatusProcessing (False,'Listagem em HTML criáda com sucesso!')

    def evtcmdStartServer(self, event): 
        self.main.start_http_server()

    def evtcmdStopServer(self, event): 
        self.main.stop_http_server

if __name__ == '__main__':
    
    rebides = Rebides()
    rebides.setLocation(100, 100)
    rebides.show()